1.先移路往左走(直到左邊是null
),刪掉當下的點(往回走),再往右下去
2.如果右邊也走到null
那就回到在上面的點
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
String ans="";
private void bfs(TreeNode root ,StringBuffer s){
if(root == null)return;
s.append((char)('a'+root.val));
if(root.left == null && root.right == null){
String tem = s.reverse().toString();
s.reverse();
if(ans=="" || ans.compareTo(tem)>0){
ans = tem;
}
}
bfs(root.right,s);
bfs(root.left,s);
s.deleteCharAt(s.length()-1);
}
public String smallestFromLeaf(TreeNode root) {
bfs(root,new StringBuffer());
return ans;
}
}